package com.ruoyi.basedata.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.basedata.domain.Concentrator;
import com.ruoyi.basedata.domain.DwAreas;
import com.ruoyi.basedata.domain.LouDong;
import com.ruoyi.basedata.domain.Room;
import com.ruoyi.basedata.mapper.ConcentratorMapper;
import com.ruoyi.basedata.mapper.DwAreasMapper;
import com.ruoyi.basedata.mapper.LouDongMapper;
import com.ruoyi.basedata.mapper.RoomMapper;
import com.ruoyi.basedata.service.IDwAreasService;
import com.ruoyi.basedata.wxentity.AreaTreaXBDosage;
import com.ruoyi.common.utils.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
//import org.springframework.util.CollectionUtils;

/**
 * 区域Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-09-06
 */
@Service
public class DwAreasServiceImpl implements IDwAreasService 
{
    @Autowired
    private DwAreasMapper dwAreasMapper;
    @Autowired
    private LouDongMapper louDongMapper;

    @Autowired
    private RoomMapper roomMapper;

    @Autowired
    private ConcentratorMapper concentratorMapper;

    /**
     * 查询区域
     * 
     * @param ID 区域主键
     * @return 区域
     */
    @Override
    public DwAreas selectDwAreasByID(Long ID)
    {
        return dwAreasMapper.selectDwAreasByID(ID);
    }

    /**
     * 查询区域列表
     * 
     * @param dwAreas 区域
     * @return 区域
     */
    @Override
    public List<DwAreas> selectDwAreasList(DwAreas dwAreas)
    {
        return dwAreasMapper.selectDwAreasList(dwAreas);
    }

    /**
     * 新增区域
     * 
     * @param dwAreas 区域
     * @return 结果
     */
    @Override
    public int insertDwAreas(DwAreas dwAreas)
    {
        List<DwAreas> dwAreasList = dwAreasMapper.selectList(new LambdaQueryWrapper<DwAreas>()
                .eq(dwAreas.getParentId() != null, DwAreas::getParentId, dwAreas.getParentId()));
        List<String> codeList = CollectionUtils.convertList(dwAreasList, DwAreas::getAreaCode);
        String newAreaCode = "";
        //如果根据pid没有查到数据那么新生成的areacode就应该为pid+0001
        if (codeList.size() == 0){
            DwAreas dwAreas1 = dwAreasMapper.selectDwAreasByID(dwAreas.getParentId());
            if (dwAreas1 == null){
                newAreaCode = "0001";
            }else {
                newAreaCode = dwAreas1.getAreaCode()+"0001";
            }
        }else {
            //不为空时就找出最大的areaCode+1
            long maxCode = Long.MIN_VALUE;
            String maxCodeString = "";
            for(String areaCode : codeList){
                long l = Long.parseLong(areaCode);
                if (l > maxCode){
                    maxCode = l;
                    maxCodeString =  areaCode;
                }
            }
            newAreaCode = String.valueOf(++maxCode);
            int length = newAreaCode.length();
            int zerosToAdd = maxCodeString.length()-length;
            for (int i = 0;i<zerosToAdd;i++){
                newAreaCode = "0" + newAreaCode;
            }
        }
        dwAreas.setAreaCode(newAreaCode);
        dwAreas.setDeleteMark(0);
        return dwAreasMapper.insertDwAreas(dwAreas);
    }

    /**
     * 修改区域
     * 
     * @param dwAreas 区域
     * @return 结果
     */
    @Override
    public int updateDwAreas(DwAreas dwAreas)
    {
        DwAreas dwAreas1 = dwAreasMapper.selectDwAreasByID(dwAreas.getID());
        if (dwAreas.getParentId().equals(dwAreas.getID())){
            return 0;
        }
        if (dwAreas.getParentId().equals(dwAreas1.getParentId())){
            return dwAreasMapper.updateDwAreas(dwAreas);
        }else {
            List<DwAreas> dwAreasList = dwAreasMapper.selectList(new LambdaQueryWrapper<DwAreas>()
                    .eq(dwAreas.getParentId() != null, DwAreas::getParentId, dwAreas.getParentId()));
            List<String> codeList = CollectionUtils.convertList(dwAreasList, DwAreas::getAreaCode);
            String newAreaCode = "";
            //如果根据pid没有查到数据那么新生成的areacode就应该为pid+0001
            if (codeList.size() == 0){
                DwAreas dwAreas2 = dwAreasMapper.selectDwAreasByID(dwAreas.getParentId());
                newAreaCode = dwAreas2.getAreaCode()+"0001";
            }else {
                //不为空时就找出最大的areaCode+1
                long maxCode = Long.MIN_VALUE;
                String maxCodeString = "";
                for(String areaCode : codeList){
                    long l = Long.parseLong(areaCode);
                    if (l > maxCode){
                        maxCode = l;
                        maxCodeString =  areaCode;
                    }
                }
                newAreaCode = String.valueOf(++maxCode);
                int length = newAreaCode.length();
                int zerosToAdd = maxCodeString.length()-length;
                for (int i = 0;i<zerosToAdd;i++){
                    newAreaCode = "0" + newAreaCode;
                }
            }
            dwAreas.setAreaCode(newAreaCode);
            return dwAreasMapper.updateDwAreas(dwAreas);
        }

    }

    /**
     * 批量删除区域
     * 
     * @param IDs 需要删除的区域主键
     * @return 结果
     */
    @Override
    public int deleteDwAreasByIDs(Long[] IDs)
    {
        List<Long> deleteList = new ArrayList<>();
        Arrays.stream(IDs).forEach(id->{
            List<DwAreas> dwAreas1 = dwAreasMapper.selectList(new LambdaQueryWrapper<DwAreas>()
                    .eq(DwAreas::getParentId, id));
            DwAreas dwAreas = dwAreasMapper.selectDwAreasByID(id);
            List<LouDong> louDongList = louDongMapper.selectList(new LambdaQueryWrapper<LouDong>()
                    .like(dwAreas.getAreaCode() != null, LouDong::getAreaCode, dwAreas.getAreaCode()));
            if (louDongList.size() == 0 && dwAreas1.size() == 0){
                deleteList.add(id);
            }
        });
        if (deleteList.size() == 0){
            return 0;
        }
        return dwAreasMapper.deleteBatchIds(deleteList);
    }

    /**
     * 删除区域信息
     * 
     * @param ID 区域主键
     * @return 结果
     */
    @Override
    public int deleteDwAreasByID(Long ID)
    {
//        louDongMapper.selectListByAreaId()
        return dwAreasMapper.deleteDwAreasByID(ID);
    }

    @Override
    public List<DwAreas> selectAreaList(Long id) {
        DwAreas dwAreas = selectDwAreasByID(id);
        return dwAreasMapper.selectList(new LambdaQueryWrapper<DwAreas>()
                .likeRight(dwAreas.getAreaCode()!=null,DwAreas::getAreaCode,dwAreas.getAreaCode())
                .orderByDesc(DwAreas::getID));
    }

    @Override
    public List<DwAreas> selectAreaList(Collection<String> areaCodes) {
        return dwAreasMapper.selectList(new LambdaQueryWrapper<DwAreas>()
                .in(!CollectionUtils.isAnyEmpty(areaCodes),DwAreas::getAreaCode,areaCodes));
    }

    @Override
    public List<DwAreas> getAreaTree() {
        List<DwAreas> dwAreas = dwAreasMapper.selectListByParentId(0L);
        setChildren(dwAreas);
        return dwAreas;
    }

    @Override
    public DwAreas selectDwAreasByAreaCode(String areaCode) {
        return dwAreasMapper.selectOne(new LambdaQueryWrapper<DwAreas>()
                .eq(DwAreas::getAreaCode,areaCode));
    }

    @Override
    public List<DwAreas> childrenList(Long id,String areaName) {
//        if (id == 0L){
//            return dwAreasMapper.selectList(new LambdaQueryWrapper<DwAreas>()
//                    .eq(DwAreas::getParentId,id));
//        }
//        DwAreas dwAreas = dwAreasMapper.selectDwAreasByID(id);
        return dwAreasMapper.selectList(new LambdaQueryWrapper<DwAreas>()
                .eq(id != null,DwAreas::getParentId,id)
                .like(areaName != null,DwAreas::getAreaName,areaName));
    }

    @Override
    public List<AreaTreaXBDosage> getAreaTreeForMobile(Integer areaId) {
        return dwAreasMapper.getAreaTreeForMobile(areaId);
    }

    private void setChildren(List<DwAreas> areas) {
        areas.forEach(dwAreas -> {
            List<DwAreas> areaDOS1 = dwAreasMapper.selectListByParentId(dwAreas.getID());
            List<LouDong> louDongList = louDongMapper.selectList(new LambdaQueryWrapper<LouDong>()
                    .eq(LouDong::getAreaCode, dwAreas.getAreaCode()));
            List<Concentrator> concentrators = concentratorMapper.selectList(new LambdaQueryWrapper<Concentrator>()
                    .eq(Concentrator::getSheetsCode, dwAreas.getAreaCode()));
            if (areaDOS1.size()>0){
                dwAreas.setChildren(areaDOS1);
            }
            dwAreas.setConcentrators(concentrators);
            if(louDongList.size()>0){
                louDongList.forEach(louDong -> {
                    Long count = roomMapper.selectCount(new LambdaQueryWrapper<Room>()
                            .eq(Room::getLdId, louDong.getLdId()));
                    louDong.setLeaf(count == 0);
                });
                dwAreas.setLouDongs(louDongList);
            }
            setChildren(areaDOS1);
        });
    }
}
